﻿<%@ Page Language="C#" ValidateRequest="False" AutoEventWireup="True" Debug="true"
    CodeBehind="NodeManagement.aspx.cs"
    Inherits="Medianamik.UI.ContentManagement.NodeManagement" 
    MasterPageFile="../MasterPages/MedianamikModule.Master" %>
<%@ Register TagPrefix="med" Namespace="Medianamik.UI.Controls" %>
<%@ Register TagPrefix="telerik" Namespace="Telerik.Web.UI" %>
<%@ Register TagPrefix="lock" Namespace="Medianamik.UI.Lock" %>
<%@ Import Namespace="Medianamik.Globalization"%>

<asp:Content ID="HeaderContent" ContentPlaceHolderID="HeaderContentHolder" runat="server">
</asp:Content>
<asp:Content ID="ContextMenuContent" ContentPlaceHolderID="ContextMenu" runat="server">
    <asp:LinkButton ID="btnSave" runat="server" CssClass="Action BtSauvegarder SuspendLock" OnClick="Save"
        Text="<%$ Resources: Medianamik_Global, SaveButtonText %>" />
    <asp:LoginView ID="SaveAndPublishLoginView"  runat="server">
        <RoleGroups>
            <asp:RoleGroup Roles="PublishContent">
                <ContentTemplate>
                    <asp:LinkButton ID="btnSaveAndPublish" runat="server" CssClass="Action BtSauvegarderApprouver SuspendLock"
                        OnClick="btnSaveAndPublish_Click" Text="<%$ Resources: Medianamik_ContentManagement, SavePublishButton %>" />
                </ContentTemplate>
            </asp:RoleGroup>
        </RoleGroups>
    </asp:LoginView>
    <asp:PlaceHolder ID="SaveAndApprovePlaceHolder" runat="server">
        <ContentTemplate>
            <asp:LinkButton ID="btnSaveAndApprove" runat="server" CssClass="Action BtSauvegarderApprouver SuspendLock"
                OnClick="btnSaveAndApprove_Click" Text="<%$ Resources: Medianamik_ContentManagement, SaveApproveButton %>" />
        </ContentTemplate>
    </asp:PlaceHolder>
    <asp:LinkButton ID="btnRevert" runat="server" CssClass="Action BtUndo" OnClick="btnRevert_Click"
        Text="<%$ Resources: Medianamik_ContentManagement, RevertButton %>" />
    <a runat="server" id="lnkCancel" class="Action BtAnnuler unlock"><asp:Literal runat="server" Text="<%$ Resources: Medianamik_Global, Cancel %>" /></a>
</asp:Content>

<asp:Content ID="MainTreeView" ContentPlaceHolderID="VariableRightPanel" runat="server">
    <asp:PlaceHolder ID="TreeviewPlaceholder" runat="server">
        <asp:UpdatePanel ID="updatePanel" UpdateMode="Always" runat="server">
            <ContentTemplate>
                <div class="BlocArbo">
                    <telerik:RadTreeView ID="treeview" OnClientNodeClicked="ClientNodeClicked" OnNodeExpand="NodeExpand"
                        runat="server" ExpandDelay="0" Height="295" Width="222" EnableEmbeddedSkins="false"
                        Skin="Medianamik" />
                    <asp:HiddenField ID="isNavigating" runat="server" Value="" />
                </div>
            </ContentTemplate>
        </asp:UpdatePanel>
    </asp:PlaceHolder>
</asp:Content>

<asp:Content ID="CenterContent" ContentPlaceHolderID="CenterContent" runat="server">
    <asp:ScriptManagerProxy ID="scriptManagerProxy" runat="server" OnNavigate="OnNavigate">
        <Services>
            <asp:ServiceReference Path="/Medianamik/Services/LockWebService.asmx" />
        </Services>
    </asp:ScriptManagerProxy>
    <asp:UpdatePanel ID="breadcrumbUpdatePanel" runat="server" UpdateMode="Conditional"
        ChildrenAsTriggers="true">
        <ContentTemplate>
            <med:BreadcrumbTrail ID="bctFilAriane" PostBackClientEvent="breadcrumbClick" runat="server"
                CssClass="FilAriane" />
        </ContentTemplate>
    </asp:UpdatePanel>
    <asp:UpdatePanel ID="fieldsPanel" runat="server" UpdateMode="Conditional">
        <ContentTemplate>
            <lock:LockInfoTooltip runat="server" ID="LockInfoTooltip" OnLockDeleted="LockInfoTooltip_LockDeleted" />
            <med:MedianamikFormTemplate ID="nodeFields" DefaultCultureName="en-ca" runat="server" />
            <asp:HiddenField ID="lockResquestFailed" runat="server" Value="" />
            <asp:HiddenField ID="backOrForward" runat="server" Value="" />
        </ContentTemplate>
    </asp:UpdatePanel>
</asp:Content>
<asp:Content ID="ExternalContent" ContentPlaceHolderID="ExternalContentHolder" runat="server">
    <telerik:RadScriptBlock runat="server" ID="scriptBlock">

        <script type="text/javascript">
            //<![CDATA[

            var nodeId = '<%=CurrentNode.ID.ToString()%>';
            
            function selectCurrentNode() {
                var treeView = $find('<%= treeview.ClientID %>');
                var nodes = treeView.get_selectedNodes();
                for (var i = 0; i < nodes.length; i++) {
                    nodes[i].set_selected(false);
                    nodes[i].unhighlight();
                }
                var node = treeView.findNodeByValue(nodeId);
                if (node != null) {
                    node.select();
                    node.set_expanded(true);
                    while (node != null && node.get_level() > 0) {
                        node = node.get_parent();
                        node.set_expanded(true);
                    }
                }
            }

            function confirmLeaveEdition() {
                if (confirm('<%= Translator.TranslateJS("Medianamik_ContentManagement", "LeaveEditionMessage") %>')) {
                    $find('<%= MedianamikFormTemplate.LockActivator.ClientID %>').set_lockStopped(true);
                    Medianamik.UI.Services.LockWebService.Unlock(nodeId);
                    window.location = '<%= ReturnURL %>';
                }
                else {
                    selectCurrentNode();
                }
            }

            function breadcrumbClick(clientid, id) {
                if (id == '<%= RootNodeId.ToString() %>') {
                    confirmLeaveEdition();
                }
                else if (confirm('<%= Translator.TranslateJS("Medianamik_ContentManagement", "SwitchNodeEditionMessage") %>')) {
                    $find('<%= MedianamikFormTemplate.LockRequester.ClientID %>').tryLock(id, 3600, function() { Navigate(id, clientid) }, function() { __doPostBack('<%= lockResquestFailed.ClientID %>', id) });
                }
                else {
                    selectCurrentNode();
                }
            }

            function ClientNodeClicked(sender, eventArgs) {
                var node = eventArgs.get_node();
                if (node.get_value() == '<%= RootNodeId.ToString() %>') {
                    confirmLeaveEdition();
                }
                else if (confirm('<%= Translator.TranslateJS("Medianamik_ContentManagement", "SwitchNodeEditionMessage") %>')) {
                    $find('<%= MedianamikFormTemplate.LockRequester.ClientID %>').tryLock(node.get_value(), 3600, function() { Navigate(node.get_value(), '<%= isNavigating.ClientID %>', node) }, function() { __doPostBack('<%= lockResquestFailed.ClientID %>', node.get_value()) });
                }
                else {
                    selectCurrentNode();
                }
            }

            function Navigate(id, clientId, node) {
                $find('<%= MedianamikFormTemplate.LockActivator.ClientID %>').set_lockStopped(true);
                Medianamik.UI.Services.LockWebService.Unlock(nodeId);
                if (node && node.get_expandMode() == 1) {
                    var isNavigating = $get(clientId);
                    isNavigating.value = id;
                    node.expand();
                }
                else {
                    __doPostBack(clientId, id);
                }
            }

            //]]></script>

    </telerik:RadScriptBlock>
</asp:Content>
